<template>
  <div class="global-tree">
    <a-tree
      :field-names="{ key: 'id', title: 'areaName', children: 'children' }"
      :tree-data="props.treeData"
      :expand-action="expandAction"
      @select="select"
      @expand="F_ExpandNode"
    >
      <template #title="node">
        <slot name="Slot-Tree-Node" :node="node.dataRef">
          <div class="global-tree-node">
            <a-tooltip>
              <template #title>{{ node.name }}</template>
              <p
                class="font-14 tree-node-title"
                @click="F_SelectedNode(node.dataRef)"
              >
                {{ node.name }}
              </p>
            </a-tooltip>

            <!-- 右侧的图标 ？ 根据具体情况判断是否需要节点显示右侧更多操作的图标-->
            <a-dropdown>
              <slot name="Slot-Menu-Icon" :node="node.dataRef"></slot>

              <template #overlay>
                <slot name="Slot-Menu-Btns" :node="node.dataRef"></slot>
              </template>
            </a-dropdown>
          </div>
        </slot>
      </template>
    </a-tree>
  </div>
</template>

<script setup lang="ts" name="global-tree">
import { watch } from "vue";

interface TsProps {
  treeData: any[];
  expandAction: string | boolean;
}

const props = withDefaults(defineProps<TsProps>(), {
  treeData: () => [],
  expandAction: false, // 目录展开逻辑，可选 false 'click' 'dblclick'
  defaultIcon: true,
});

const emits = defineEmits<{
  (e: "select", data: number | string): void; // 重置搜索条件
}>();

const select = (e: Array<number>): void => {
  emits("select", e[0]);
};

// 遍历循环treeData
// eslint-disable-next-line func-style, @typescript-eslint/explicit-function-return-type
function F_LoopTreeData(data: any) {
  if (data) {
    for (let node of data) {
      node.key = node.id;
      node.class = "tree-node-item"; // 设置class名称
      node.expandTools = true; // 根据具体情况判断是否需要节点显示右侧更多操作的图标, 默认可不需要此属性或者将此属性置为false

      // 最底级节点去掉展开按钮
      if (!node.childList || (node.childList && !node.childList.length)) {
        // node.isLeaf = true;  使用loadData时有用
        node.isLastNode = true; // 代表无展开符号的最后一层节点, 使用自定义图标
      }

      if (node.childList && !!node.childList.length) {
        F_LooptreeData(node.childList);
      }
    }
  }
}

// 节点勾选
const F_CheckedNode = (node: any): void => {
  //
};

// 节点点击
const F_SelectedNode = (node: TsTreeNodeObj): void => {
  // console.log('节点点击--', node);
};

// 节点展开
const F_ExpandNode = (
  expandedKeys: Array<string | number>,
  expandInfo: any
): void => {
  // const { expanded, node } = expandInfo;
  console.log("节点展开--expandedKeys, expanded, node--", expandedKeys);
};

// 节点右侧拓展操作
const F_MoreActionNode = (node: any): void => {
  //
};

const watchStop = watch(
  () => props.treeData,
  () => {
    F_LoopTreeData(props.treeData);
  },
  { immediate: true }
);
onUnmounted(() => {
  watchStop();
});
</script>

<style lang="less">
.global-tree {
  .ant-tree-treenode {
    margin-bottom: 12px;
    padding: 0;
    line-height: 32px !important;

    &:hover {
      border-radius: 4px;
      background: @mask03 !important;
    }

    &:hover .expand-dropdown-link {
      display: block;
    }
  }

  .ant-tree {
    background: transparent !important;

    .ant-tree-switcher {
      svg {
        color: #ccc;
      }
    }
  }

  .ant-tree-list-holder-inner {
    display: block !important;
  }

  .ant-tree-switcher {
    margin-top: 5px;
  }

  .ant-tree,
  .ant-tree-node-content-wrapper {
    margin-right: 10px;
    background: transparent !important;
  }

  .ant-tree-treenode-selected {
    border-radius: 4px;
    line-height: 36px !important;
    background: rgba(48, 93, 194, 0.2) !important;
  }

  :where(.css-dev-only-do-not-override-1t97qq9).ant-tree .ant-tree-treenode {
    padding: 0 !important;
  }

  :where(.css-dev-only-do-not-override-kqecok).ant-tree .ant-tree-switcher {
    position: relative;
    flex: none;
    align-self: center;
    width: 24px;
    margin: 0;
    line-height: 24px;
    text-align: center;
    cursor: pointer;
    user-select: none;
  }

  .tree-node-item {
    display: flex;
    width: 100%;
    padding: 0;
    line-height: 22px;
    align-items: center !important;
  }

  .global-tree-node {
    display: flex;
    width: 100%;
    // height: 36px;
    line-height: 36px;
    align-items: center !important;
    justify-content: space-between;

    .tree-node-title {
      display: flex;
      overflow: hidden;
      height: 32px;
      // width: 100px;
      // margin-right: 8px;
      border-radius: 4px;
      line-height: 32px;
      white-space: nowrap;
      text-overflow: ellipsis;
      color: @text02;
      // flex: 1;
      align-items: center;
      gap: 4px;
      align-self: stretch;

      &:active {
        background-color: @mask03;
      }
    }

    .expand-dropdown-link {
      width: 30px;
      height: 30px;
      border-radius: 4px;
      line-height: 30px;
      text-align: center;
      color: @text03;

      &:hover {
        background-color: @mask01;
      }
    }
  }
}
</style>
